package com.dawn.java.jvm.keyword;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Ruffianjiang
 * @version V1.0
 * @Title: VolatileTest
 * @Package java4learn
 * @date 2019/7/28 9:31
 */
@Slf4j
public class VolatileTest {

    private volatile static int num = 0;

    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 100; i++) {
            Thread thread1 = new Thread(new Task1(i));
            thread1.start();
        }
    }

    static class Task1 implements Runnable {

        int index = 0;

        Task1(int index) {
            this.index = index;
        }

        @Override
        public void run() {
            log.info("{} - before - {}", num, Thread.currentThread().getName());
            num = index;
            log.info("{} - normal - {}", num, Thread.currentThread().getName());
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("{} - sleep - {}", num, Thread.currentThread().getName());
        }

    }

}
